﻿//    Copyright 2010,2011 Himanshu Vasishth
//   
//    This file is part of SecretVault.
//
//    SecretVault is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SecretVault is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with SecretVault.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace Common
{
    public static class Log
    {
        public static bool Enabled
        {
            get;
            private set;
        }

        public static TraceLevel TraceLevel
        {
            get;
            set;
        }

        public static void EnableTracing()
        {
            // No need to create another listener if it is already enabled
            if (Enabled)
            {
                return;
            }
            Enabled = true;
            string fileName = Path.Combine(Path.GetTempPath(), 
                                    "SecretVault" + Guid.NewGuid() + ".log");

            // Create a trace listener and add it
            TextWriterTraceListener myListener = new TextWriterTraceListener(fileName);
            Trace.Listeners.Add(myListener);
        }

        private static TraceWindow traceWindow;
        public static void ShowTraceWindow()
        {
            if (traceWindow != null && traceWindow.Visible)
            {
                return;
            }
            traceWindow = new TraceWindow();
            traceWindow.Show();
        }

        public static void WriteIf(TraceLevel level, string component, string message, params object [] args )
        {
            if (!Enabled)
            {
                return;
            }

            string formattedMessage = FormatMessage(component, message);
            Trace.WriteLineIf(level <= TraceLevel, string.Format(formattedMessage, args));
            if (traceWindow != null)
            {
                traceWindow.WriteLine(level, component, formattedMessage, args);
            }
        }

        /// <summary>
        /// Returns the message in the format {component}::{CurrentTime}
        /// </summary>
        /// <param name="componentFlag"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        private static string FormatMessage(string component, string message)
        {
            return string.Format("{0}::{1}::{2}::{3}", 
                component, 
                DateTime.UtcNow, 
                Thread.CurrentThread.ManagedThreadId,
                message);
        }
    }
}
